//$(function () { plusReady();})
function plusReady() {
    login();
}
if (window.plus) {
    plusReady();
} else {
    document.addEventListener('plusready', plusReady, false);
}

function login() {
    //test1 abcabc
    $("#login").on("click", function () {

        //验证
        if ("" == $("#name").val() || "请输入手机号码" == $("#name").val()) {
            plus.nativeUI.alert("请填写用户名", function () { }, "提示", "好");
            return false;
        }
        if ("" == $("#password").val() || "请输入密码" == $("#password").val()) {
            plus.nativeUI.alert("请输入密码", function () { }, "提示", "好");
            return false;
        }

		var postdata = { "name": $("#name").val(),"password": $("#password").val() };
//		console.log(JSON.stringify(postdata));
//		return false;
        var jqXHR = $.ajax({
            type: 'POST',
            url: configManager.RequstUrl + 'api/user/login',
            data: postdata,
            beforeSend:function(){ $("#waitingupload").removeClass("heisebghid").addClass("heisebg");}
        }).done(function (data) {
        	$("#waitingupload").removeClass("heisebg").addClass("heisebghid");
            if ("success" == data.state) {
                var loginuser = data.data;

                //if ([2, 3].indexOf(loginuser.user_type) > -1) {
                //// 完善用户店铺信息
                //GetUserStoreInfo(loginuser);
                //}

                console.log("用户登录:" + JSON.stringify(loginuser));
                if (!loginuser.token || loginuser.token.trim() == "") { plus.nativeUI.alert("注册失败（无效Token）"); return; }
                plus.storage.setItem(storageManager.user, JSON.stringify(loginuser));
                var opener = plus.webview.currentWebview().opener();
                opener.reload(false);
                if (opener.id != pageName.mine) {                	                // 更新我的
                    plus.webview.getWebviewById(pageName.mine).reload(false);
                }

                back();
            } else {
                plus.nativeUI.alert(data.message, function () { }, "提示", "好");
            }
        }).fail(function () {
            plus.nativeUI.alert(errorMessage.interface);
        });

        //var login		= plus.webview.currentWebview();
        //	login.close("auto", 10);

    });
}

// 获取用户店铺信息
function GetUserStoreInfo(loginuser) {
    $.ajax({
        type: "GET",
        async: false,
        url: configManager.RequstUrl + "api/user/profile",
        data: { "userid": loginuser.id, "account": "1" }
    }).done(function (data) {
        if ("success" == data.state) {
            loginuser.store = data.data.store;
        }
        else {
            console.log(data.message);
        }

    });
}
